<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app"></div>
    <script src="./react/react.js"></script>
    <script src="./react/react-dom.js"></script>
    <script src="./react/babel.js"></script>
    <script type="text/babel">
        class Fn extends React.Component {
            constructor() {
                super()
                this.state = {
                    name: '',
                    age: '',
                    list: [{ name: "占山", age: 45, id: 4565 }, { name: "磺酸钠", age: 45, id: 45885 }],
                    flag: true,
                    id: 0
                }
            }
            add = () => {
                let arr = JSON.parse(JSON.stringify(this.state.list));
                arr.push({ name: this.state.name, age: this.state.age, id: +new Date() })

                this.setState({
                    name: "",
                    age: '',
                    list: arr,

                })

            }
            del(id) {
                let arr = JSON.parse(JSON.stringify(this.state.list));
                let i = arr.findIndex((item) => {
                    return item.id == id
                })
                arr.splice(i, 1)
                this.setState({
                    list: arr
                })
            }
            Input = (e) => {
                this.setState({
                    [e.target.name]: e.target.value
                })
            }
            edit = (e) => {
                let arr = JSON.parse(JSON.stringify(this.state.list));
                let i = arr.findIndex((item) => {
                    return item.id == e
                })
                this.setState({
                    name: this.state.list[i].name,
                    age: this.state.list[i].age,
                    flag: false,
                    id: i
                })
            }
            editOk = () => {
                let arr = JSON.parse(JSON.stringify(this.state.list));
                arr[this.state.id].name = this.state.name
                arr[this.state.id].age = this.state.age
                this.setState({
                    name: "",
                    age: '',
                    list: arr,
                    flag: true,
                    id: 0
                })
            }
            render() {
                return (
                    <div>
                        <input type="text" name='name' value={this.state.name} onChange={this.Input} />
                        <input type="text" name='age' value={this.state.age} onChange={this.Input} />
                        {this.state.flag ? <button onClick={this.add}>提交</button> : <button onClick={this.editOk}>确定修改</button>}
                        <table border='1px' width='300px' text-align='center'>
                            <thead>
                                <tr >
                                    <th>姓名</th>
                                    <th>年龄</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {
                                    this.state.list.length > 0 ? this.state.list.map((v, i) => {

                                        return (
                                            <tr key={v.id}>
                                                <td>
                                                    {v.name}
                                                </td>
                                                <td>
                                                    {v.age}
                                                </td>
                                                <td>
                                                    <button onClick={() => this.edit(v.id)}>修改</button>
                                                    <button onClick={() => this.del(v.id)}>删除</button>
                                                </td>
                                            </tr>
                                        )
                                    }) : <tr><td colSpan='3' >暂无数据</td></tr>
                                }
                            </tbody>
                        </table>
                    </div>

                )
            }
        }
        ReactDOM.render(
            <div><Fn /></div>,
            document.getElementById('app')
        )
    </script>
</body>

</html>